package cz.muni.fi.pv168.ContactManager;

//import org.apache.tomcat.dbcp.dbcp.BasicDataSource;

import org.apache.tomcat.dbcp.dbcp.BasicDataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * Created by IntelliJ IDEA.
 * User: vlado
 * Date: 4.6.2012
 * Time: 15:03
 * To change this template use File | Settings | File Templates.
 */
public class DBUtil {
    private static boolean dbmade = false;
    private static BasicDataSource dataSource;
    final static Logger log = LoggerFactory.getLogger("Test cz.muni.fi.pv168.ContactManager.PersonManager");

    public static void restartDB(DataSource dataSource)
    {
        PreparedStatement st1 = null;
        PreparedStatement st2 = null;
        PreparedStatement st3 = null;
        PreparedStatement st4 = null;
        Connection connection = null;
        try
        {
            Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
            connection = dataSource.getConnection();
            st1 = connection.prepareStatement("DROP TABLE CONTACT" );
            st1.execute();
            st2 = connection.prepareStatement("DROP TABLE PERSON");
            st2.execute();
            st3 = connection.prepareStatement("CREATE TABLE PERSON (" +
                    "ID INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY," +
                    "FIRSTNAME VARCHAR(50)," +
                    "LASTNAME VARCHAR(50) )" );
            st3.execute();
            st4 = connection.prepareStatement("CREATE TABLE CONTACT (\n" +
                    "    ID INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY,\n" +
                    "    PERSONID INTEGER REFERENCES PERSON (ID),\n" +
                    "    CONTACTVALUE VARCHAR(50),\n" +
                    "    CONTACTTYPE VARCHAR(50) )");
            st4.execute();
        }
        catch(SQLException ex)
        {
            log.error("Error when creating testing tables", ex);
        }
        catch(ClassNotFoundException ex)
        {
            log.error("Error when loading driver class", ex);
        }
        finally
        {
            try
            {
                if(st1 !=null) {st1.close();}
                if(st2 !=null) {st2.close();}
                if(st3 !=null) {st3.close();}
                if(st4 !=null) {st4.close();}
                if(connection != null) {connection.close();}
            }
            catch(SQLException ex)
            {
                log.error("Error when closing statements.", ex);
            }
        }
    }

    private static void makeDB()
    {
        if(dbmade)
        {
            return;
        }
        try{
            Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
        }
        catch(ClassNotFoundException ex)
        {
            log.error("Driver class not found!");
        }
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setUrl("jdbc:derby://localhost:1527/MojeDB");
        dataSource.setUsername("vlado");
        dataSource.setPassword("parada");
        DBUtil.restartDB(dataSource);

        DBUtil.dataSource = dataSource;
    }

    public static BasicDataSource getDataSource()
    {
        if(!dbmade)
        {
            makeDB();
        }
        return DBUtil.dataSource;
    }
}
